#ifndef FONTENGINE
#define FONTENGINE

#include "GE_Base.h"

namespace Syp
{
///Basic Font class. Has buffering capabilities which only submits it's buffred font geometry once per frame.Many Fonts can be created.
class SYPAPI Font
{
	friend class FontRegistry;
public:
	/**Creates a new Font and returns a pointer to it.
	@par
		A name for the font. This name must be unique.
	**/
	static Font* createFont(const String& fontName);
	///Deletes a Font. While Fonts are not Resources, they are managed by a FontRegistry which will delete all fonts upon the release of Engine. However if a certain font is not required during runtime and you would like to free up some RAM/VRAM, you may destroy a font during runtime.
	static void destroyFont(Font* font);
	///Returns the font's name
	const String& getFontName();
	/**Initialises fonts
	@remarks	
		Initialises a font from a texture file, fontWidths file, Color	
	@par
		The texture filename which is holding the font image.
	@par
		The FontWidths file.
	@par 
		Color of font.
	**/
	void initFonts(const String& fileName, int size, int MARGIN = 5);
	///Releases a Font and all Resources(Meshes/Textures) it is using.
	void release();
	///Clears the buffer from the last frame
	void clearBuffer();
	///Sends the data buffered to the Renderer for rendering
	void sendData();
	///Buffers text in a Array and draws it in 1 go for efficiency
	void draw(const String &text,float x,float y,float z);	
	///Basically returns a processed text within a width specified, upon reaching the end, it goes to the next line. This function helps to locate the parts where the newline needs to be added and adds them there. It is more efficient to process the text once and draw it many times instead of passing in the text every frame and have it process the newlines every frame;
	String wordWrap(const String &text,float width);
	///Not to be called by user, use DestroyFont(Font* f) instead. 
	~Font();
	///Generates a static mesh and returns a MeshInstance using it instead of drawing it. This is useful for text which does not change every frame(GUI Text, Labels, etc...). Access to the actual mesh can be obtained by using MeshInstance->getMesh(); New : An optional MeshInstance can be passed in which will be used as the MeshInstance pointing to the generated static mesh instead of creating a new one.
	MeshInstance* generateStaticMesh(const String& text,float x,float y,float z,MeshInstance* fmi = 0);
	///returns the width the text will take up
	inline float fontWidth(const String& text);
	///Returns the Margin value used to generate this Font
	PInt getMargin();
	///Strips formatting information from String and returns plain text.
	static String stripFormatting(const String& text);
private:	
	///The free type variable holding the font
	FT_Face     face;
	///The unique font name.
	String name;	
	///Number of characters which will fit in the specified width of text
	inline PInt numOfChars(const String& text,float width);
	///Array that stores the font widths
	ShortArray fontWidths;	
	///The MeshInstance of the font mesh.
	MeshInstance* m;
	///Data that is sent to this font is buffered 
	NIndexedMesh* fontData;
	Font(const String& name);
	///Size of each character
	float fontSizeX;
	float fontSizeY;
	PInt Margin;

};
}

#endif

